//
//  EXFirstThirdViewController.m
//  Exercise
//
//  Created by 羊谦 on 16/6/3.
//  Copyright © 2016年 NetEase-yangqian. All rights reserved.
//

#import "EXFirstThirdViewController.h"

@interface EXFirstThirdViewController ()

@property (nonatomic,strong) CALayer *layer;

@end

@implementation EXFirstThirdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage *image = [UIImage imageNamed:@"login_bg"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:image];
    //    self.view.backgroundColor = [UIColor whiteColor];
    _layer = [[CALayer alloc] init];
    _layer.bounds = CGRectMake(0, 0, 20, 20);
    _layer.position = CGPointMake(50, 150);
    _layer.anchorPoint = CGPointMake(0.6, 0.5);
    _layer.contents = (id)[UIImage imageNamed:@"login_hook"].CGImage;
    [self.view.layer addSublayer:_layer];
    
    //    [self translationAnimation];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


//普通动画
- (void)translationAniation:(CGPoint)location {
    //创建并指定动画属性
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    //    animation.fromValue = [NSNumber numberWithInteger:50];
    animation.toValue = [NSValue valueWithCGPoint:location];
    animation.duration = 2.0;
    
    
    animation.delegate = self;
    
    //存储当前位置在动画结束后使用
    [animation setValue:[NSValue valueWithCGPoint:location] forKey:@"KCBasicAnimationLocation"];
    
    //animation.repeatCount = 10;//重复次数
    //animation.removedOnCompletion = NO;//运行一次去除动画
    
    [_layer addAnimation:animation forKey:@"KCBasicAnimation_Translation"];
    
}

//#pragma mark 关键帧动画
//-(void)translationAnimation {
//    //1.创建关键帧动画并设置动画属性
//    CAKeyframeAnimation *keyframeAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
//
//    //2.设置关键帧,这里有四个关键帧
//    NSValue *key1=[NSValue valueWithCGPoint:_layer.position];//对于关键帧动画初始值不能省略
//    NSValue *key2=[NSValue valueWithCGPoint:CGPointMake(80, 220)];
//    NSValue *key3=[NSValue valueWithCGPoint:CGPointMake(45, 300)];
//    NSValue *key4=[NSValue valueWithCGPoint:CGPointMake(55, 400)];
//    NSArray *values=@[key1,key2,key3,key4];
//    keyframeAnimation.values=values;
//    //设置其他属性
//    keyframeAnimation.duration=8.0;
//    keyframeAnimation.beginTime=CACurrentMediaTime()+2;//设置延迟2秒执行
//
//
//    //3.添加动画到图层，添加动画后就会执行动画
//    [_layer addAnimation:keyframeAnimation forKey:@"KCKeyframeAnimation_Position"];
//}

//贝塞尔曲线绘制

#pragma mark 关键帧动画(贝塞尔)
-(void)translationAnimation {
    //1.创建关键帧动画并设置动画属性
    CAKeyframeAnimation *keyframeAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    //2.设置路径
    //绘制贝塞尔曲线
    CGMutablePathRef path=CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, _layer.position.x, _layer.position.y);//移动到起始点
    CGPathAddCurveToPoint(path, NULL, 160, 280, -30, 300, 55, 400);//绘制二次贝塞尔曲线
    
    keyframeAnimation.path=path;//设置path属性
    CGPathRelease(path);//释放路径对象
    //设置其他属性
    keyframeAnimation.duration=4.0;
    keyframeAnimation.beginTime=CACurrentMediaTime()+1;//设置延迟2秒执行
    
    
    //3.添加动画到图层，添加动画后就会执行动画
    [_layer addAnimation:keyframeAnimation forKey:@"KCKeyframeAnimation_Position"];
}

#pragma mark 旋转动画
-(void)rotationAnimation {
    //1.创建动画并指定动画属性
    CABasicAnimation *basicAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    
    //2.设置动画属性初始值、结束值
    //    basicAnimation.fromValue=[NSNumber numberWithInt:M_PI_2];
    basicAnimation.toValue=[NSNumber numberWithFloat:M_PI*2];
    
    //设置其他动画属性
    basicAnimation.duration=1.0;
    basicAnimation.autoreverses=NO;//旋转后再旋转到原来的位置
    basicAnimation.repeatCount=HUGE_VALF;//设置无限循环
    basicAnimation.removedOnCompletion=NO;
    
    
    //4.添加动画到图层，注意key相当于给动画进行命名，以后获得该动画时可以使用此名称获取
    [_layer addAnimation:basicAnimation forKey:@"KCBasicAnimation_Rotation"];
}

//
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //    UITouch *touch = touches.anyObject;
    //    CGPoint point = [touch locationInView:self.view];
    
    //判断是否已经常见过动画，如果已经创建则不再创建动画
    CAAnimation *animation= [_layer animationForKey:@"KCBasicAnimation_Translation"];
    if(animation) {
        if (_layer.speed==0) {
            [self animationResume];
        }else{
            [self animationPause];
        }
    }else{
        //创建并开始动画
        //        [self translationAniation:point];
        
        [self rotationAnimation];
    }
    
    //    [self translationAniation:point];
    //    [self rotationAnimation];
}

#pragma mark 动画暂停
-(void)animationPause {
    //取得指定图层动画的媒体时间，后面参数用于指定子图层，这里不需要
    CFTimeInterval interval=[_layer convertTime:CACurrentMediaTime() fromLayer:nil];
    //设置时间偏移量，保证暂停时停留在旋转的位置
    [_layer setTimeOffset:interval];
    //速度设置为0，暂停动画
    _layer.speed=0;
}

#pragma mark 动画恢复
-(void)animationResume {
    //获得暂停的时间
    CFTimeInterval beginTime= CACurrentMediaTime()- _layer.timeOffset;
    //设置偏移量
    _layer.timeOffset=0;
    //设置开始时间
    _layer.beginTime=beginTime;
    //设置动画速度，开始运动
    _layer.speed=1.0;
}


#pragma mark - 动画代理方法
#pragma mark 动画开始
-(void)animationDidStart:(CAAnimation *)anim {
    NSLog(@"animation(%@) start.\r_layer.frame=%@",anim,NSStringFromCGRect(_layer.frame));
    NSLog(@"%@",[_layer animationForKey:@"KCBasicAnimation_Translation"]);//通过前面的设置的key获得动画
}

#pragma mark 动画结束
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    NSLog(@"animation(%@) stop.\r_layer.frame=%@",anim,NSStringFromCGRect(_layer.frame));
    //开启事务
    [CATransaction begin];
    //禁用隐式动画
    [CATransaction setDisableActions:YES];
    
    _layer.position=[[anim valueForKey:@"KCBasicAnimationLocation"] CGPointValue];
    
    //提交事务
    [CATransaction commit];
}

@end
